﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace OpenPasteSpider.EntityFrameworkCore
{

    /// <summary>
    /// 
    /// </summary>
    public class SqliteDbContextFactory : IDesignTimeDbContextFactory<SqliteDbContext>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public SqliteDbContext CreateDbContext(string[] args)
        {
            //Console.WriteLine("PgsqlDbContextFactory.CreateDbContext");

            //啥时候执行的，执行了多少次?
            //AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
            var configuration =OpenPasteSpiderEntityFrameworkCoreModule.BuildConfiguration(null);

            var builder = new DbContextOptionsBuilder<SqliteDbContext>()
                .UseSqlite(configuration.GetConnectionString(OpenPasteSpiderDbProperties.SqliteConnectionStringName));
            //    .ReplaceService<IMigrationsSqlGenerator, MyMigrationsSqlGenerator>();

            return new SqliteDbContext(builder.Options);
        }

        //private static IConfigurationRoot BuildConfiguration()
        //{
        //    var builder = new ConfigurationBuilder()
        //        .SetBasePath(Directory.GetCurrentDirectory())
        //        .AddJsonFile("appsettings.json", optional: false)
        //        .AddJsonFile($"appsettings.Development.json", optional: true)
        //        .AddEnvironmentVariables();

        //    return builder.Build();
        //}
    }
}
